//1.导入vue 和 vue-router 的包
import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from "@/views/Login.vue"

//2.调用vue.use() 函数，把 VueRouter 安装为 Vue 的插件
//vue.use()函数的作用，就是来安装插件的
Vue.use(VueRouter)

//3.创建路由的实例对象
const router = new VueRouter({
    routes: [
        {
            path: "/",
            redirect: "/login"
        }, {
            path: "/login",
            name: "login",
            component: Login
        }, {
            path: "/index",
            name: "index",
            component: () => import("@/views/Index.vue"),
            meta: {
                title: "首页"
            },
            redirect: "/home",
            children: [
                {
                    path: "/home",
                    name: "home",
                    component: () => import("@/views/Home.vue"),
                    meta: {
                        title: "首页"
                    },
                }
            ]
        },

    ]
})

router.beforeEach(async (to, from, next) => {
    const token = localStorage.getItem('token')

    to.path == "/login" ? token ? next("/index") : next() : token ? next() : next("/login")
})


//4.向外共享路由的实例对象
export default router